package com.hronline;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;

import com.delegate.ManagerEmployeesDelegate;

public class ManagerEmployeesStackActivity extends FragmentActivity implements ManagerEmployeesDelegate {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.fragmentstackctivity);
		showEmployeePage();
	}
	
	void addFragment(Fragment fragment, boolean addToBackStack, int transition) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.simple_fragment, fragment);
        ft.setTransition(transition);
        if (addToBackStack)
            ft.addToBackStack(null);
        ft.commit();
    }

//    public void launchNewActivity(View v) {
//        Intent intent = new Intent(v.getContext(), ExampleActivity.class);
//        startActivity(intent);
//    }

    public void finishFragmentOrActivity() {
        FragmentManager manager = getSupportFragmentManager();
        if (manager.getBackStackEntryCount() > 0)
            getSupportFragmentManager().popBackStack();
        else
            finish();
    }

    public void launchNewFragment(Fragment fragment) {
        addFragment(fragment, true, FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    }
	
    
    /* ========================================================================== */
    /* ===============================SELECTOR=================================== */
    /* ========================================================================== */
    private void showEmployeePage(){
    	launchNewFragment(new EmployeesActivity(this, this));
    }
    
    /* ========================================================================== */
    /* ===============================DELEGATE=================================== */
    /* ========================================================================== */
    @Override
	public void openLoginPage() {
    	// TODO Auto-generated method stub
		/* reset all user data caching */
		Constant.resetAllUserDataCaching(this, MODE_PRIVATE);
		
		Intent intent	=	new Intent(this, LoginActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		this.startActivity(intent);
		
		/* destroy the current activity */
		finish();
	}
    
    @Override
    public void pop() {
    	// TODO Auto-generated method stub
    	finishFragmentOrActivity();
    }
    
    
    
    /* 
     * EMPLOYEE DETAIL PAGE
     *  */
    @Override
    public void openEmployeeDetail(String employeeId, String employeeName) {
    	// TODO Auto-generated method stub
		launchNewFragment(new EmployeeDetailActivity(this, this, employeeId, employeeName));
    }

	@Override
	public void openLogLatenessPage(String employeeName, String employeeId) {
		// TODO Auto-generated method stub
		launchNewFragment(new LogLatenessActivity(this, this, employeeId, employeeName));
	}

	@Override
	public void openLogSicknessPage(String employeeName, String employeeId) {
		// TODO Auto-generated method stub
		launchNewFragment(new LogSicknessActivity(this, this, employeeId, employeeName));
	}
}
